package com.nzy.controller;

import com.nzy.common.ResultData;
import com.nzy.controller.request.GoodsRequest;
import com.nzy.controller.request.ListGoodsRequest;
import com.nzy.controller.response.ListGoodsResponse;
import com.nzy.entity.Goods;
import com.nzy.service.GoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 添加商品
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultData add(GoodsRequest goodsRequest){
        Goods goods = goodsService.queryBySkuId(goodsRequest.getSkuId());
        if(goods != null){
            return ResultData.fail("当前商品已存在");
        }
        goods = new Goods();
        BeanUtils.copyProperties(goodsRequest, goods);
        goodsService.insert(goods, goodsRequest.getNumber());
        return ResultData.success();
    }

    /**
     * 商品列表，展示库存
     */
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResultData query(ListGoodsRequest listGoodsRequest){
        List<ListGoodsResponse> list = goodsService.queryList(listGoodsRequest);
        return ResultData.success(list);
    }

    /**
     * 商品详情
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public ResultData detail(Integer id){
        Goods goods = goodsService.queryById(id);
        if(goods != null){
            return ResultData.fail("当前商品不存在");
        }
        return ResultData.success(goods);
    }
}
